package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Notice;
import cn.wolfcode.domain.Permission;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

public class UserContext {
    private static final String USER_IN_SESSION = "USER_IN_SESSION";
    private static final String PERMISSION_IN_SESSION = "PERMISSION_IN_SESSION";
    private static final String NOTICE_IN_SESSION = "NOTICE_IN_SESSION";
    private static String UUID;

    //获取UUID
    public static String getUUID(){
        return UUID;
    }
    //设置UUID
    public static void setUUID(String uuid){
        UUID = uuid;
    }


    public static HttpSession getSession() {
        ServletRequestAttributes attrs =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attrs.getRequest().getSession();
    }

    public static HttpServletRequest getRequest() {
        ServletRequestAttributes attrs =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attrs.getRequest();
    }


    public static Notice getCurrentNotice(){
        return (Notice) UserContext.getSession().getAttribute("NOTICE_IN_SESSION");
    }

    public static void setCurrentNotice(List<Notice> notice){
        UserContext.getSession().setAttribute(UserContext.NOTICE_IN_SESSION,notice);
    }


    public static Employee getCurrentUser(){
        return (Employee) UserContext.getSession().getAttribute("USER_IN_SESSION");
    }

    public static void setCurrentUser(Employee employee){
        UserContext.getSession().setAttribute(UserContext.USER_IN_SESSION,employee);
    }

    public static List<Permission> getCurrentPermissions(){
        return (List<Permission>) UserContext.getSession().getAttribute(UserContext.PERMISSION_IN_SESSION);
    }

    public static void setCurrentPermissions(List<Permission> permissions){
        UserContext.getSession().setAttribute(UserContext.PERMISSION_IN_SESSION,permissions);
    }

    public static String getRealIp(HttpServletRequest request) {
        // 这个一般是Nginx反向代理设置的参数
        String ip = request.getHeader("X-Real-IP");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Forwarded-For");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        // 处理多IP的情况（只取第一个IP）
        if (ip != null && ip.contains(",")) {
            String[] ipArray = ip.split(",");
            ip = ipArray[0];
        }
        return ip;
    }

}
